"""
Case Type   : DATAVEC模块
Case Name   : enable_pq参数打开关闭来回切换，枚举参数值验证
Create At   : 2025/4/10
Owner       : @peilinqian
Description :
    0、查看enable_pq初始配置值;
    1、修改参数enable_pq为0;
    2、重启数据库，使参数生效;
    3、alter system方式修改参数enable_pq为1;
    4、重启数据库，使参数生效;
    5、修改参数enable_pq为false;
    6、重启数据库，使参数生效;
    7、修改参数enable_pq为true;
    8、重启数据库，使参数生效;
    9、guc修改enable_pq为默认值;
    10、重启数据库，使参数生效;
    11、查询数据库状态;
Expect      :
    0、查看enable_pq初始配置值;
    1、修改参数enable_pq为0; 成功
    2、重启数据库，重启成功，参数生效;
    3、alter system方式修改参数enable_pq为1; 成功
    4、重启数据库，重启成功，参数生效;
    5、修改参数enable_pq为false; 成功
    6、重启数据库，重启成功，参数生效;
    7、修改参数enable_pq为true; 成功
    8、重启数据库，重启成功，参数生效;
    9、guc修改enable_pq为默认值; 成功
    10、重启数据库，重启成功，参数生效;
    11、查询数据库状态; 状态正常
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

pri_dbuser = Node(node='PrimaryDbUser')
pq_lib_path = '/usr/local/sra_recall/lib/'
log = Logger()
log.info("----判断本地环境是否存在pq插件----")
check_cmd = f'if [ -d {pq_lib_path} ]; ' \
            f'then echo "True"; else echo "False"; fi'
log.info(check_cmd)
check_result = pri_dbuser.sh(check_cmd).result()
log.info(check_result)


@unittest.skipIf(check_result == 'False',
                 '----环境不具备PQ插件，跳过执行用例----')
class DataVecTestCase(unittest.TestCase):
    def setUp(self):
        log.info(f'----{os.path.basename(__file__)}:start----')
        self.sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.pq_env_name = 'DATAVEC_PQ_LIB_PATH'
        step_txt = '----step0:设置pq环境变量; expect:成功----'
        log.info(step_txt)
        check_env_cmd = f'if grep -q "{self.pq_env_name}" ' \
                        f'"{macro.DB_ENV_PATH}"; ' \
                        f'then echo "True" ; else echo "False" ;fi'
        log.info(check_env_cmd)
        check_env_result = pri_dbuser.sh(check_env_cmd).result()
        log.info(check_env_result)
        if check_env_result == 'False':
            results = []
            log.info('--设置pq环境变量--')
            set_env_cmd = f"echo 'export {self.pq_env_name}={pq_lib_path}' " \
                          f">> {macro.DB_ENV_PATH}"
            try:
                result = pri_dbuser.sh(set_env_cmd).result()
                log.info(f"{pri_dbuser.name} 执行结果: {result}")
                results.append(result)
            except Exception as e:
                log.error(f"{pri_dbuser.name} 执行异常: {str(e)}")
                results.append(f"ERROR: {str(e)}")
        check_env_result = pri_dbuser.sh(check_env_cmd).result()
        log.info(check_env_result)
        self.assertEqual(check_env_result, 'True', '执行失败:' + step_txt)

        step_txt = '----step0:查看enable_pq初始配置值;----'
        log.info(step_txt)
        self.init_para = self.com.show_param("enable_pq")

    def test_main(self):
        step_txt = '----step1:修改参数enable_pq为0; expect:成功----'
        log.info(step_txt)
        msg = self.sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_pq= 0")
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step2:重启数据库，使参数生效; expect:重启成功----'
        log.info(step_txt)
        self.sh.stop_db_instance()
        start_result = self.sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_result,
                      '执行失败:' + step_txt)
        log.info('----查询参数----')
        self.new_para = self.com.show_param("enable_pq")
        self.assertEqual(self.new_para, 'off', '执行失败:' + step_txt)

        step_txt = '----step3:alter system方式修改参数enable_pq为1; ' \
                   'expect:成功----'
        log.info(step_txt)
        alter_sql = 'alter system set enable_pq to 1;'
        result = self.sh.execut_db_sql(alter_sql)
        log.info(result)
        self.assertIn(self.constant.alter_system_success_msg, result,
                      '执行失败:' + step_txt)

        step_txt = '----step4:重启数据库，使参数生效; expect:重启成功，参数生效;----'
        log.info(step_txt)
        self.sh.stop_db_instance()
        start_result = self.sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_result,
                      '执行失败:' + step_txt)
        log.info('----查询参数----')
        self.new_para = self.com.show_param("enable_pq")
        self.assertEqual(self.new_para, 'on', '执行失败:' + step_txt)

        step_txt = '----step5:修改参数enable_pq为false; expect:成功----'
        log.info(step_txt)
        msg = self.sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_pq= false")
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step6:重启数据库，使参数生效; expect:重启成功，参数生效;----'
        log.info(step_txt)
        self.sh.stop_db_instance()
        start_result = self.sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_result,
                      '执行失败:' + step_txt)
        log.info('----查询参数----')
        self.new_para = self.com.show_param("enable_pq")
        self.assertEqual(self.new_para, 'off', '执行失败:' + step_txt)

        step_txt = '----step7:修改参数enable_pq为true; expect:成功----'
        log.info(step_txt)
        msg = self.sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_pq= 1")
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step8:重启数据库，使参数生效; expect:重启成功，参数生效;----'
        log.info(step_txt)
        self.sh.stop_db_instance()
        start_result = self.sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_result,
                      '执行失败:' + step_txt)
        log.info('----查询参数----')
        self.new_para = self.com.show_param("enable_pq")
        self.assertEqual(self.new_para, 'on', '执行失败:' + step_txt)

        step_txt = '----step9:guc修改enable_pq为默认值; expect:成功----'
        log.info(step_txt)
        msg = self.sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_pq")
        self.assertTrue(msg, '执行失败:' + step_txt)

        step_txt = '----step10:重启数据库，使参数生效; expect:重启成功，参数生效;----'
        log.info(step_txt)
        self.sh.stop_db_instance()
        start_result = self.sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, start_result,
                      '执行失败:' + step_txt)
        log.info('----查询参数----')
        self.new_para = self.com.show_param("enable_pq")
        self.assertEqual(self.new_para, 'off', '执行失败:' + step_txt)

        step_txt = '----step11:查询数据库状态; expect:状态正常----'
        log.info(step_txt)
        status_result = self.sh.get_db_cluster_status('status')
        self.assertTrue(status_result, '执行失败:' + step_txt)

    def tearDown(self):
        step_txt = '----this is teardown----'
        log.info(step_txt)
        step1_txt = '----teardown:还原参数enable_pq为初始值; ' \
                    'expect:修改成功----'
        log.info(step1_txt)
        msg = self.sh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f"enable_pq={self.init_para}")
        step2_txt = '----teardown:重启数据库，使参数生效; expect:重启成功----'
        log.info(step2_txt)
        self.sh.stop_db_instance()
        start_result = self.sh.start_db_instance()

        log.info(f'----{os.path.basename(__file__)}:end----')
        self.assertTrue(msg, '执行失败:' + step_txt + step1_txt)
        self.assertTrue(start_result, '执行失败:' + step_txt + step2_txt)
